<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/4/19
 * Time: 10:31 AM
 */

namespace App\Models\CacheModel\zl5;

use App\Models\CacheModel\chat\HzQuestion;
use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl5ExplainTitle extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl5_explain_title';

    protected $cacheModel = 'explain.explain_question';

    protected $qtype = [
        // 问题ID => 问题类型
        1 => 8, // 我适合跟什么样的人谈恋爱?
        2 => 8, // 有哪些特质的人比较吸引我?
        3 => 8, // 我跟结婚对象会怎样相遇?
        4 => 8, // 在婚姻中什么样的伴侣比较适合我？
        5 => 6, // 我会遇到什么样的领导?
        6 => 6, // 我适合从事什么领域的工作?
        7 => 9, // 如何获得正财?
        8 => 9, // 我能通过哪些方式获得意外之财？
        9 => 13, // 我的人生追求是什么？
        10 => 11, // 别人对我的第一印象是什么？
    ];

    /**
     * 获取问题 新类型
     * @param $qid
     * @return int|mixed
     */
    public function getQuestionType($qid)
    {
        return $this->qtype[$qid] ?? 0;
    }

    public function getQuestionData()
    {
        $explainTypes = (new Zl5ExplainType())->geTypes();

        $allQuestions = $this->getAllQuestionList();

        $resData = [];

        foreach ($allQuestions as $qid => $questionItem)
        {
            if (!isset($resData[$questionItem['type_id']]))
            {
                if (!isset($explainTypes[$questionItem['type_id']]))
                {
                    continue;
                }
                $resData[$questionItem['type_id']] = [
                    'title' => $explainTypes[$questionItem['type_id']]['title'],
                    'q_list' => []
                ];
            }

            $resData[$questionItem['type_id']]['q_list'][] = [
                'id' => $questionItem['id'],
                'title' => $questionItem['title'],
            ];
        }

        return $resData;
    }

    public function getQuestionData2()
    {
        $hzQuestionConfig = config('hz_question');
        $allQuestions = $this->getAllQuestionList();
        $allQuestions2 = (new HzQuestion())->getAllQuestionList();
        $data = [];
        foreach ($allQuestions as $qid => $questionItem)
        {
            $questionItem['type_id'] = $this->getQuestionType($qid);
            if (!isset($data[$questionItem['type_id']]))
            {
                if (!isset($hzQuestionConfig[$questionItem['type_id']]))
                {
                    continue;
                }
                $data[$questionItem['type_id']] = [
                    'title' => $hzQuestionConfig[$questionItem['type_id']],
                    'q_list' => []
                ];
            }

            $data[$questionItem['type_id']]['q_list'][] = [
                'id' => $questionItem['id'],
                'type' => 1,
                'title' => $questionItem['title'],
            ];
        }
        foreach ($allQuestions2 as $qid => $questionItem)
        {
            if (!isset($data[$questionItem['question_type']]))
            {
                if (!isset($hzQuestionConfig[$questionItem['question_type']]))
                {
                    continue;
                }
                $data[$questionItem['question_type']] = [
                    'title' => $hzQuestionConfig[$questionItem['question_type']],
                    'q_list' => []
                ];
            }

            $data[$questionItem['question_type']]['q_list'][] = [
                'id' => $questionItem['id'],
                'type' => 2,
                'title' => $questionItem['question'],
            ];
        }

        $resData = [];
        foreach ($hzQuestionConfig as $typeId => $typeName)
        {
            if (isset($data[$typeId]))
            {
                $resData[] = $data[$typeId];
            }
        }

        return $resData;
    }

    /**
     * 获取所有的问题信息
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getAllQuestionList()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setSort('DESC')->ZsGetIocData('list', function ()
        {
            try
            {
                $data = $this->where(['status' => 1])->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);
        return $this->batchGetInfoByIds(array_keys($ids));
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids], 'status' => 1];
        $field = 'title,extend,sort,type_id,basis_easy';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $id => $itemData)
        {
            $resData[$id] = arrayKeyData2int($itemData, ['id', 'sort', 'type_id']);

            if (isset($itemData['extend']))
            {
                $extend = $itemData['extend'] ? json_decode($itemData['extend'], true) : [];

                if (isset($extend['recommend_goods']))
                {
                    $ext = [];
                    foreach ($extend['recommend_goods'] as $recommendGoodItem)
                    {
                        if (isset($recommendGoodItem['status']) && (int)$recommendGoodItem['status'] === 1)
                        {
                            $ext[] = [
                                'title' => $recommendGoodItem['title'] ?? '',
                                'des' => $recommendGoodItem['des'] ?? '',
                                'sort' => (int)($recommendGoodItem['sort'] ?? 0),
                                'target' => $recommendGoodItem['target'] ?? '',
                                'link' => $recommendGoodItem['link'] ?? '',
                                'status' => 1
                            ];
                        }
                    }

                    if ($ext)
                    {
                        $ext = resort($ext, 'sort');
                    }
                    $resData[$id]['extend'] = ['recommend_goods' => $ext];
                }
            }
        }

        return $resData;
    }


}
